﻿Imports System.Data.Linq

Public Class Dinner
    Public ReadOnly Property IsValid() As Boolean
        Get
            Return GetRuleViolations.Count = 0
        End Get
    End Property

    Public Function GetRuleViolations() As IEnumerable(Of RuleViolation)
        Dim result As New List(Of RuleViolation)
        If String.IsNullOrEmpty(Title) Then
            result.Add(New RuleViolation("Title required", "Title"))
        End If
        If String.IsNullOrEmpty(Description) Then
            result.Add(New RuleViolation("Description required", "Description"))
        End If
        If String.IsNullOrEmpty(HostedBy) Then
            result.Add(New RuleViolation("HostedBy required", "HostedBy"))
        End If
        If String.IsNullOrEmpty(Address) Then
            result.Add(New RuleViolation("Address required", "Address"))
        End If
        If String.IsNullOrEmpty(Country) Then
            result.Add(New RuleViolation("Country required", "Country"))
        End If
        If String.IsNullOrEmpty(ContactPhone) Then
            result.Add(New RuleViolation("Phone# required", "ContactPhone"))
        End If
        If Not PhoneValidator.IsValidNumber(ContactPhone, Country) Then
            result.Add(New RuleViolation("Phone# does not match country", "ContactPhone"))
        End If
        Return result
    End Function

    Private Sub OnValidate(ByVal action As ChangeAction)
        If Not IsValid Then
            Throw New ApplicationException("Rule violations prevent saving")
        End If
    End Sub
End Class
